package com.jbit.examSystem.controller;

import com.github.pagehelper.PageHelper;
import com.jbit.examSystem.param.PageParam;
import com.jbit.examSystem.pojo.ResponseResult;
import com.jbit.examSystem.pojo.TableDataInfo;
import com.jbit.examSystem.service.ScoreService;
import com.jbit.examSystem.vo.ExamInfoVo;
import com.jbit.examSystem.pojo.ScoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

@RequestMapping("score")
@Controller

public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @RequestMapping("saveExamInfo")
    @ResponseBody
    public ResponseResult saveExamInfo(@RequestBody ExamInfoVo examInfoVo) throws IOException {
        ResponseResult result = null;
        try {
            scoreService.saveExamInfo(examInfoVo.getScore(), examInfoVo.getExamDetails());
            result = ResponseResult.success(null);
        } catch (Exception e){
            e.printStackTrace();
            result = ResponseResult.error(e.getMessage());
        }

        return result;
    }

    @RequestMapping("list")
    @ResponseBody
    public TableDataInfo list(int stuNo, PageParam page){
        TableDataInfo respData = new TableDataInfo();

        try {
            PageHelper.startPage(page.getPageNum(),page.getPageSize(),true,true,false);
            List<ScoreVo> scoreVos = scoreService.list(stuNo);

            respData.succeed(scoreVos, 0);
        } catch (Exception e){
            respData.setCode(500);
            e.printStackTrace();
        }
        return respData;
    }

//   private List<ScoreVo> convert2ScoreVo(List<Score> scores) {
//        List<ScoreVo> scoreVos = new ArrayList<>();
//        for (Score score : scores) {
//            ScoreVo scoreVo = new ScoreVo();
//            scoreVo.setAmount(score.getAmount());
//            scoreVo.setScoreId(score.getId());
//            scoreVo.setExamTime(score.getExam().getExamTime());
//            scoreVo.setOkAmount(score.getOkAmount());
//            scoreVo.setTitle(score.getExam().getTitle());
//            scoreVos.add(scoreVo);
//        }
//
//        return scoreVos;
//    }
}
